import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_core/app_core.dart';
import 'package:yh_core_flutter_project/project/common/intl/mine_intl.dart';
import '../../../common/constant/index.dart';
import '../../../router/app_routers.dart';
import '../../../common/managers/user_manager.dart';

class MinePage extends StatelessWidget {
  const MinePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Get.theme.colorScheme.background,
      appBar: MyAppBar(
        MineIntl.mine.tr,
        showBack: false,
        titleColor: Get.theme.colorScheme.onSurface,
        actions: [
          AppGesture(
            onTap: () {
              Get.log('设置');
            },
            child: Padding(padding: EdgeInsets.all(8.rs), child: Icon(Icons.settings, color: Get.theme.colorScheme.onSurface, size: 24.rs)),
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [_buildUserInfo(), SizedBox(height: 20.hs), _buildMenuSection(), SizedBox(height: 20.hs), _buildSystemSection(), SizedBox(height: 40.hs)],
        ),
      ),
    );
  }

  Widget _buildUserInfo() {
    return Obx(() {
      final userManager = Get.find<UserManager>();
      return Container(
        margin: EdgeInsets.all(16.rs),
        padding: EdgeInsets.all(20.rs),
        decoration: BoxDecoration(
          color: Get.theme.colorScheme.surface,
          borderRadius: BorderRadius.circular(12.rs),
          boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10, offset: Offset(0, 2))],
        ),
        child: Row(
          children: [
            Container(
              width: 60.rs,
              height: 60.rs,
              decoration: BoxDecoration(shape: BoxShape.circle, color: Get.theme.colorScheme.primary.withOpacity(0.1)),
              child: Icon(Icons.person, size: 30.rs, color: Get.theme.colorScheme.primary),
            ),
            SizedBox(width: 16.ws),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    userManager.isLoggedIn ? userManager.userModel?.userId ?? '' : MineIntl.notLoggedIn.tr,
                    style: AppStyles.s18b.copyWith(color: Get.theme.colorScheme.onSurface),
                  ),
                  SizedBox(height: 4.hs),
                  Text(
                    userManager.isLoggedIn ? userManager.userModel?.nickName ?? ''  : '点击登录',
                    style: AppStyles.s14.copyWith(color: Get.theme.colorScheme.onSurface.withOpacity(0.7)),
                  ),
                ],
              ),
            ),
            Icon(Icons.chevron_right, color: Get.theme.colorScheme.onSurface.withOpacity(0.5), size: 24.rs),
          ],
        ),
      );
    });
  }

  Widget _buildMenuSection() {
    final menuItems = [
      _MenuItem(icon: Icons.share, title: MineIntl.shareDevice.tr, onTap: () => Get.log('分享设备')),
      _MenuItem(icon: Icons.home_outlined, title: MineIntl.home.tr, onTap: () => Get.log('家庭管理')),
      _MenuItem(icon: Icons.feedback_outlined, title: MineIntl.feedback.tr, onTap: () => Get.log('意见反馈')),
      _MenuItem(icon: Icons.support_outlined, title: MineIntl.support.tr, onTap: () => Get.log('客服支持')),
      _MenuItem(icon: Icons.chat_bubble_outline, title: 'SmartDialog 测试', onTap: () => Get.toNamed(AppRouters.smartDialogTest.r)),
    ];

    return _buildSection('功能', menuItems);
  }

  Widget _buildSystemSection() {
    final systemItems = [
      _MenuItem(icon: Icons.security, title: MineIntl.systemPermissions.tr, onTap: () => Get.log('系统权限')),
      _MenuItem(icon: Icons.privacy_tip_outlined, title: MineIntl.privacyPolicyManagement.tr, onTap: () => Get.log('隐私政策')),
      _MenuItem(icon: Icons.system_update, title: MineIntl.versionUpdate.tr, subtitle: 'v1.0.0', onTap: () => Get.log('版本更新')),
      _MenuItem(icon: Icons.info_outline, title: MineIntl.about.tr, onTap: () => Get.log('关于我们')),
      _MenuItem(icon: Icons.logout, title: MineIntl.logOut.tr, isDestructive: true, onTap: () => _showLogoutDialog()),
    ];

    return _buildSection('设置', systemItems);
  }

  Widget _buildSection(String title, List<_MenuItem> items) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.ws),
      decoration: BoxDecoration(color: Get.theme.colorScheme.surface, borderRadius: BorderRadius.circular(12.rs)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(16.ws, 16.hs, 16.ws, 8.hs),
            child: Text(title, style: AppStyles.s14w5.copyWith(color: Get.theme.colorScheme.onSurface.withOpacity(0.7))),
          ),
          ...items.asMap().entries.map((entry) {
            final index = entry.key;
            final item = entry.value;
            return Column(
              children: [
                _buildMenuItem(item),
                if (index < items.length - 1) Divider(height: 1, indent: 52.ws, color: Get.theme.colorScheme.onSurface.withOpacity(0.1)),
              ],
            );
          }).toList(),
        ],
      ),
    );
  }

  Widget _buildMenuItem(_MenuItem item) {
    return AppGesture(
      onTap: item.onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.ws, vertical: 12.hs),
        child: Row(
          children: [
            Icon(item.icon, size: 24.rs, color: item.isDestructive ? Colors.red : Get.theme.colorScheme.onSurface.withOpacity(0.7)),
            SizedBox(width: 12.ws),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(item.title, style: AppStyles.s16.copyWith(color: item.isDestructive ? Colors.red : Get.theme.colorScheme.onSurface)),
                  if (item.subtitle != null) Text(item.subtitle!, style: AppStyles.s12.copyWith(color: Get.theme.colorScheme.onSurface.withOpacity(0.5))),
                ],
              ),
            ),
            Icon(Icons.chevron_right, color: Get.theme.colorScheme.onSurface.withOpacity(0.3), size: 20.rs),
          ],
        ),
      ),
    );
  }

  void _showLogoutDialog() {
    Get.dialog(
      AlertDialog(
        title: Text('确认退出'),
        content: Text('确定要退出登录吗？'),
        actions: [
          TextButton(onPressed: () => Get.back(), child: Text(MineIntl.cancel.tr)),
          TextButton(
            onPressed: () {
              Get.back();
              Get.find<UserManager>().logout();
              // 跳转到登录页面
              Get.offAllNamed(AppRouters.login.r);
            },
            child: Text(MineIntl.logOut.tr, style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );
  }
}

class _MenuItem {
  final IconData icon;
  final String title;
  final String? subtitle;
  final VoidCallback onTap;
  final bool isDestructive;

  _MenuItem({required this.icon, required this.title, this.subtitle, required this.onTap, this.isDestructive = false});
}
